﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GEAR_Structure;
namespace Gear_Base
{
    public partial class MaterialForm : Form
    {
        private Project project;
        private Materiale selectedmaterial;
        private int state;
        private Configuration config;

        public MaterialForm(Project _project)
        {
            InitializeComponent();
            config = Configuration.getInstance();
            this.project = _project;
            loadProject();
            disableEdit();
            bindCombo();
        }

        private void smi_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loadProject()
        {
            foreach (Category_material cm in project.Materials)
            {
                TreeNode node = new TreeNode(cm.Description);
                node.Tag = cm;
                treeMaterial.Nodes[0].Nodes.Add(node);
                foreach (Materiale m in cm.ListMaterial)
                {
                    TreeNode nodeMaterial = new TreeNode(m.Name);
                    nodeMaterial.Tag = m;
                    node.Nodes.Add(nodeMaterial);
                }
            }
        }

        private void insertCategory(Category_material cm)
        {
            TreeNode node = new TreeNode(cm.Description);
            node.Tag = cm;
            treeMaterial.Nodes[0].Nodes.Add(node);
            project.Materials.Add(cm);
            foreach (Materiale m in cm.ListMaterial)
            {
                TreeNode nodeMaterial = new TreeNode(m.Name);
                nodeMaterial.Tag = m;
                node.Nodes.Add(nodeMaterial);
            }
        }

        private void creaNuovaCategoriaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MaterialNewCategoryForm frm = new MaterialNewCategoryForm();
            Category_material cm = frm.getValue(this);
            if (cm != null)
                insertCategory(cm);
        }

        private void cms_tree_Opening(object sender, CancelEventArgs e)
        {
            switch (treeMaterial.SelectedNode.Level)
            {
                case 0:
                    creaNuovaCategoriaToolStripMenuItem.Enabled = true;
                    rinominaCategoriaToolStripMenuItem.Enabled = false;
                    eliminaCategoriaToolStripMenuItem.Enabled = false;
                    inserisciNuovoMaterialeToolStripMenuItem.Enabled = false;
                    modificaMaterialeToolStripMenuItem.Enabled = false;
                    rimuoviMaterialeToolStripMenuItem.Enabled = false;
                    break;
                case 1:
                    creaNuovaCategoriaToolStripMenuItem.Enabled = false;
                    rinominaCategoriaToolStripMenuItem.Enabled = true;
                    eliminaCategoriaToolStripMenuItem.Enabled = (treeMaterial.SelectedNode.Nodes.Count==0);
                    inserisciNuovoMaterialeToolStripMenuItem.Enabled = true;
                    modificaMaterialeToolStripMenuItem.Enabled = false;
                    rimuoviMaterialeToolStripMenuItem.Enabled = false;
                    break;
                case 2:
                    creaNuovaCategoriaToolStripMenuItem.Enabled = false;
                    rinominaCategoriaToolStripMenuItem.Enabled = false;
                    eliminaCategoriaToolStripMenuItem.Enabled = false;
                    inserisciNuovoMaterialeToolStripMenuItem.Enabled = false;
                    modificaMaterialeToolStripMenuItem.Enabled = true;
                    rimuoviMaterialeToolStripMenuItem.Enabled = true;
                    break;
            }
        }

        private void rinominaCategoriaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Category_material cm = (Category_material)treeMaterial.SelectedNode.Tag;
            MaterialNewCategoryForm frm = new MaterialNewCategoryForm(cm);
            cm = frm.getValue(this);
            treeMaterial.SelectedNode.Text = cm.Description;
        }

        private void eliminaCategoriaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            TreeNode selected = treeMaterial.SelectedNode;
            if (DialogResult.Yes == MessageBox.Show("Sicuri di voler eliminare la Categoria " + selected.Text + "?",config.NomeProgrammaCompleto,MessageBoxButtons.YesNo,MessageBoxIcon.Information))
            {
                Category_material cm = (Category_material)selected.Tag;
                selected.Remove();
                project.Materials.Remove(cm);
            }
        }

        private void inserisciNuovoMaterialeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            state = 1;
            enableEdit();
            selectedmaterial = new Materiale();
            bindMaterial(selectedmaterial);
            chkequivalent_CheckedChanged(null, null);
        }

        private void modificaMaterialeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            state = 2;
            selectedmaterial = (Materiale)treeMaterial.SelectedNode.Tag;
            bindMaterial(selectedmaterial);
            enableEdit();
        }

        private void bindCombo()
        {
            cmbTipology.DataBindings.Clear();
            cmbTipology.DataSource = EnumHelper.ToList(typeof(Materiale.typology_material));
            cmbTipology.DisplayMember = "Value";
            cmbTipology.ValueMember = "Key";
            cmbTipology.SelectedIndex = -1;
        }

        private void bindMaterial(Materiale m)
        {
            cmbTipology.SelectedItem = (Materiale.typology_material)m.Typology;
            cmbTipology.SelectedValue = m.Typology;
            
            chkequivalent.DataBindings.Clear();
            chkequivalent.DataBindings.Add(new Binding("Checked", m, "ConductivityEquivalent"));

            txtDescr.DataBindings.Clear();
            txtDescr.DataBindings.Add(new Binding("Text", m, "Name"));

            txtConductivity.DataBindings.Clear();
            txtConductivity.DataBindings.Add(new Binding("Text", m, "Conductivity"));

            txtConduttance.DataBindings.Clear();
            txtConduttance.DataBindings.Add(new Binding("Text", m, "Conduttance"));

            txtTickness.DataBindings.Clear();
            txtTickness.DataBindings.Add(new Binding("Text", m, "Tickness"));

            txtDensity.DataBindings.Clear();
            txtDensity.DataBindings.Add(new Binding("Text", m, "Density"));

            txtHeat.DataBindings.Clear();
            txtHeat.DataBindings.Add(new Binding("Text", m, "SpecificHeat"));

            txtVaporMax.DataBindings.Clear();
            txtVaporMax.DataBindings.Add(new Binding("Text", m, "VaporPermeabilityMin", true, DataSourceUpdateMode.OnValidation, "E3"));

            txtVaporMin.DataBindings.Clear();
            txtVaporMin.DataBindings.Add(new Binding("Text", m, "VaporPermeabilityMax", true, DataSourceUpdateMode.OnValidation, "E3"));

            txtFatResIgro.DataBindings.Clear();
            txtFatResIgro.DataBindings.Add(new Binding("Text", m, "HygroscopicResistnaceFactor"));

            txtDinamicStiffness.DataBindings.Clear();
            txtDinamicStiffness.DataBindings.Add(new Binding("Text", m, "DynamicStiffness"));
        }


        private void button1_Click(object sender, EventArgs e)
        {
            disableEdit();
            selectedmaterial.Typology = (Materiale.typology_material)cmbTipology.SelectedValue; 
            switch (this.state)
            {
                case 1:
                    insertMaterial(selectedmaterial);
                    break;
                case 2:
                    treeMaterial.SelectedNode.Text = ((Materiale)treeMaterial.SelectedNode.Tag).Name;
                    break;
            }
        }

        private void insertMaterial(Materiale m)
        {
            Category_material cm  = (Category_material)treeMaterial.SelectedNode.Tag;
            cm.ListMaterial.Add(m);
            TreeNode nodeMaterial = new TreeNode(m.Name);
            nodeMaterial.Tag = m;
            treeMaterial.SelectedNode.Nodes.Add(nodeMaterial);
        }



        private void enableEdit()
        {
            cmbTipology.Enabled = true;
            txtDescr.Enabled = true;
            txtConductivity.Enabled = true;
            txtConduttance.Enabled = true;
            txtTickness.Enabled = true;
            txtDensity.Enabled = true;
            txtHeat.Enabled = true;
            txtVaporMax.Enabled = true;
            txtVaporMin.Enabled = true;
            txtFatResIgro.Enabled = true;
            txtDinamicStiffness.Enabled = true;
            treeMaterial.Enabled = false;
            button1.Enabled = true;
            button2.Enabled = true;
            chkequivalent.Enabled = true;
        }

        private void disableEdit()
        {
            txtDescr.Enabled = false;
            txtConductivity.Enabled = false;
            txtTickness.Enabled = false;
            txtConduttance.Enabled = false;
            txtDensity.Enabled = false;
            txtHeat.Enabled = false;
            txtVaporMax.Enabled = false;
            txtVaporMin.Enabled = false;
            txtFatResIgro.Enabled = false;
            txtDinamicStiffness.Enabled = false;
            treeMaterial.Enabled = true;
            button1.Enabled = false;
            button2.Enabled = false;
            cmbTipology.Enabled = false;
            chkequivalent.Enabled = false;
        }

        private void treeMaterial_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 2)
            {
                Materiale m = (Materiale)e.Node.Tag;
                bindMaterial(m);
                condEqVisible(m.ConductivityEquivalent);
            }
        }

        private void chkequivalent_CheckedChanged(object sender, EventArgs e)
        {
            if (!treeMaterial.Enabled)
            {
                condEqEnabled(chkequivalent.Checked);
                condEqVisible(chkequivalent.Checked);
            }
        }

        private void condEqVisible(bool value)
        {
            txtConduttance.Visible = value;
            txtTickness.Visible = value;
            label11.Visible = value;
            label3.Visible = value;
        }

        private void condEqEnabled(bool value)
        {
            txtConductivity.Enabled = !value;
            txtTickness.Enabled = value;
            txtConduttance.Enabled = value;
        }

        private void treeMaterial_MouseDown(object sender, MouseEventArgs e)
        {
            treeMaterial.SelectedNode = treeMaterial.GetNodeAt(e.Location);
            TreeNode selected = treeMaterial.SelectedNode;
            if (selected != null && selected.Level == 2)
                treeMaterial.DoDragDrop(selected.Tag, DragDropEffects.All);
        }

        private void cmbTipology_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if()
        }

    }
}
